//
//  LGServiceStatisticsController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/8.
//  服务统计

#import "LGServiceStatisticsController.h"
#import "LGSeviceStatisticsHeader.h"
#import "LGSeviceStatisticsCell.h"
#import "LGPickerView.h"
@interface LGServiceStatisticsController ()<UITableViewDataSource,UITableViewDelegate,LGPickerViewDelegate>
/**   */
@property (nonatomic , strong)UIImageView *bgView;
/**   */
@property (nonatomic , strong)UIButton *selectBtn;
/**   */
@property (nonatomic , strong)LGSeviceStatisticsHeader *headerView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *dateArray;
/**   */
@property (nonatomic , strong)NSArray *pickerArray;
/**   */
@property (nonatomic , copy)NSString *selectStr;
/**   */
@property (nonatomic , copy)NSString *type;

@end

@implementation LGServiceStatisticsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"历史统计";
    self.fd_prefersNavigationBarHidden = YES;
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.navigationView];
    [self.view addSubview:self.selectBtn];
    [self.view addSubview:self.tableView];
    self.selectStr = @"本年";
    [self requestPickerData];
}

-(void)requestData{
    [RequestUtil withPOST:@"doctorviplists/doctor/my.index/dataStat" parameters:@{@"type":self.type} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [self.selectBtn setTitle:returnData[@"date"] forState:UIControlStateNormal];
            [self.selectBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
            self.headerView.dataDic = returnData;
            self.dataArray = returnData[@"teams"];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

-(void)requestPickerData{
    [RequestUtil withPOST:@"/api/common.dict/dataStatPeriod" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dateArray = [NSArray arrayWithArray:returnData];
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSInteger i=0; i < self.dateArray.count; i++) {
                NSDictionary *tempDic = self.dateArray[i];
                [tempArray addObject:LGNSString(tempDic[@"title"])];
                if(i == 0){
                    self.selectStr = LGNSString(tempDic[@"title"]);
                    self.type = LGNSString(tempDic[@"value"]);
                    [self requestData];
                }
            }
            self.pickerArray = tempArray;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)selectBtnAction{
    if (self.pickerArray.count == 0) {
        [self requestPickerData];
    }else{
        LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[self.pickerArray];
        pickerView.selectStr = self.selectStr;
        pickerView.delegate = self;
        [self.view.window addSubview:pickerView];
    }
}

-(void)selectPickerViewResult:(NSArray *)resultArray{
    NSString *result = LGNSString(resultArray[0]);
    self.selectStr = result;
    NSInteger index = [self.pickerArray indexOfObject:result];
    self.type = LGNSString(self.dateArray[index][@"value"]);
    [self requestData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSeviceStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = self.dataArray[indexPath.row][@"title"];
    cell.dataDic = self.dataArray[indexPath.row][@"date_stat"];
    return cell;
}

-(UIImageView *)bgView{
    if(!_bgView){
        _bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"服务统计-背景"]];
        _bgView.frame = CGRectMake(0, 0, Screen_W, viewPix(396));
    }
    return _bgView;
}

-(UIButton *)selectBtn{
    if(!_selectBtn){
        _selectBtn = [UIButton buttonWithTitle:@"2023年" titleFont:LGFont(15) textColor:@"#333333" imageName:@"grayTipDown2"];
        _selectBtn.frame = CGRectMake(viewPix(20), topBarHeight, viewPix(120), viewPix(40));
        _selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_selectBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
        [_selectBtn addTarget:self action:@selector(selectBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(LGSeviceStatisticsHeader *)headerView{
    if(!_headerView){
        _headerView = [[LGSeviceStatisticsHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(265))];
    }
    return _headerView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(40), Screen_W, Screen_H-topBarHeight-viewPix(40)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSeviceStatisticsCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(130);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
